package com.leapp.sk.util

import android.app.Activity
import cc.vv.lkbasecomponent.util.LKSPUtil
import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import cc.vv.lkimcomponent.lkim.bean.body.LKIMTxtBody
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMDirect
import cc.vv.lkimcomponent.lkim.operate.LKIMEdit
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.view.DialogListView
import com.example.util.ClipboardUtil
import com.example.im.IMInsert
import com.example.im.IMReceiveService
import com.example.im.SKIMSend
import com.example.im.server.im.IMChatService
import com.example.util.finallist.KEY_VOICEPLAY_ISOUTSIDE
import com.example.util.finallist.SP_NAME
import com.example.util.finallist.SkKey
import com.leapp.sk.util.im.IMVoicePlayer

/**
 * IM消息操作
 * @author  chaohaipeng  on 2018/7/31.
 */
class IMDialogUtil {
    private object mHolder {
        val instance = IMDialogUtil()
    }
    companion object {
        fun getInstance(): IMDialogUtil {
            return mHolder.instance
        }
    }

    /**
     * 长按事件
     */
    fun longClickEvent(context: Activity,mIMMessage: LKIMMessage) {
        IMVoicePlayer.getInstance().stop()
        val dialogListObjs = IMChatService.getInstance().initIMAdapterDialog(mIMMessage)
        DialogListView.getInstance().initDialog(context, dialogListObjs, object : DialogListView.OperateInter {
            override fun onItemClick(dialogListObj: DialogListView.DialogListObj) {
                when (dialogListObj.type) {
                //复制
                    SkKey.BTKEY_INT_0
                    -> imCopy(mIMMessage)
                //转发
                    SkKey.BTKEY_INT_1
                    -> {
                    }
                //删除
                    SkKey.BTKEY_INT_2
                    -> imDelete(mIMMessage)
                //撤回
                    SkKey.BTKEY_INT_3
                    -> imWithdraw(mIMMessage)
                //听筒
                    SkKey.BTKEY_INT_4
                    -> {
                        LKSPUtil.getInstance(SP_NAME).put(KEY_VOICEPLAY_ISOUTSIDE, false)
                    }
                //外方
                    SkKey.BTKEY_INT_5
                    -> {
                          LKSPUtil.getInstance(SP_NAME).put(KEY_VOICEPLAY_ISOUTSIDE, true)
                    }
                }
            }

        })


    }
    /**
     * 撤销操作
     */
    private fun imWithdraw(mIMMessage: LKIMMessage) {
        try {
            val account = mIMMessage.getToAccount()
            SKIMSend.getInstance().cmdRepeal(account, mIMMessage.getMsgId(), mIMMessage.getLKIMChatType(), object : SKIMSend.CmdSendInter {
                override fun onSuccess(lkimMessage: LKIMMessage) {
                    LKIMEdit.getInstance().delete(account, mIMMessage.getMsgId())
                    val informMessage = IMInsert.getInstance().insertInformMessage(account, "你撤回了一条消息", System.currentTimeMillis(), mIMMessage.getLKIMChatType())
                    IMReceiveService.getInstance().sendInsertRepeal(account, mIMMessage.getMsgId(), informMessage)
                }

                override fun faile(lkimMessage: LKIMMessage) {
                    LKToastUtil.showToastShort("撤回失败")
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    /**
     * 删除
     */
    private fun imDelete(mIMMessage: LKIMMessage) {
        try {
            val account: String
            if (LKIMDirect.RECEIVE == mIMMessage.getLKIMDirect() && LKIMChatType.SingleChat == mIMMessage.getLKIMChatType()) {
                account = mIMMessage.getFromAccount()
            } else {
                account = mIMMessage.getToAccount()
            }
            LKIMEdit.getInstance().delete(account, mIMMessage.getMsgId())
            IMReceiveService.getInstance().sendDelete(account, mIMMessage.getMsgId())
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    /**
     * 复制
     */
    private fun imCopy(mIMMessage: LKIMMessage) {
        try {
            val lkIMBody = mIMMessage.getLkIMBody() as LKIMTxtBody
            ClipboardUtil.getInstance().copy(lkIMBody.message)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }


}